home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Edit List and Spin / Transform / MatrixMethods.cs < prev    next >
Encoding:
Text File  |  2001-01-15  |  5.9 KB  |  162 lines

  1. //--------------------------------------------
  2. // MatrixMethods.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8.  
  9. class MatrixMethods: Form
  10. {
  11.      Matrix          matrix; 
  12.      Button          btnInvert;
  13.      NumericUpDown[] updown = new NumericUpDown[3];
  14.      RadioButton[]   radio = new RadioButton[2];
  15.  
  16.      public MatrixMethods() 
  17.      {
  18.           Text = "Matrix Methods";
  19.           FormBorderStyle = FormBorderStyle.FixedDialog;
  20.           ControlBox      = false;
  21.           MinimizeBox     = false;
  22.           MaximizeBox     = false;
  23.           ShowInTaskbar   = false;
  24.           Location        = ActiveForm.Location +
  25.                             SystemInformation.CaptionButtonSize +
  26.                             SystemInformation.FrameBorderSize;
  27.  
  28.           String[] astrLabel = { "X / DX:", "Y / DY:", "Angle:" };
  29.  
  30.           for (int i = 0; i < 3; i++)
  31.           {
  32.                Label label    = new Label();
  33.                label.Parent   = this;
  34.                label.Text     = astrLabel[i];
  35.                label.Location = new Point(8, 8 + 16 * i);
  36.                label.Size     = new Size(32, 8);
  37.  
  38.                updown[i] = new NumericUpDown();
  39.                updown[i].Parent        = this;
  40.                updown[i].Location      = new Point(40, 8 + 16 * i);
  41.                updown[i].Size          = new Size(48, 12);
  42.                updown[i].TextAlign     = HorizontalAlignment.Right;
  43.  
  44.                updown[i].DecimalPlaces = 2;
  45.                updown[i].Increment = 0.1m;
  46.                updown[i].Minimum = Decimal.MinValue;
  47.                updown[i].Maximum = Decimal.MaxValue;
  48.           }
  49.                // Create group box and radio buttons.
  50.  
  51.           GroupBox grpbox = new GroupBox();
  52.           grpbox.Parent   = this;
  53.           grpbox.Text     = "Order";
  54.           grpbox.Location = new Point(8, 60);
  55.           grpbox.Size     = new Size(80, 32);
  56.  
  57.           for (int i = 0; i < 2; i++)
  58.           {
  59.                radio[i] = new RadioButton();
  60.                radio[i].Parent = grpbox;
  61.                radio[i].Text = new string[] { "Prepend", "Append" } [i];
  62.                radio[i].Location = new Point(8, 8 + 12 * i);
  63.                radio[i].Size     = new Size(50, 10);
  64.                radio[i].Checked = (i == 0);
  65.           }
  66.  
  67.                // Create 8 buttons for terminating dialog.
  68.  
  69.           string[] astrButton = { "Reset", "Invert", "Translate", "Scale",
  70.                                   "Rotate", "RotateAt", "Shear", "Cancel" };
  71.  
  72.           EventHandler[] aeh = { new EventHandler(ButtonResetOnClick), 
  73.                                  new EventHandler(ButtonInvertOnClick),
  74.                                  new EventHandler(ButtonTranslateOnClick), 
  75.                                  new EventHandler(ButtonScaleOnClick),
  76.                                  new EventHandler(ButtonRotateOnClick), 
  77.                                  new EventHandler(ButtonRotateAtOnClick),
  78.                                  new EventHandler(ButtonShearOnClick) };
  79.  
  80.           for (int i = 0; i < 8; i++)
  81.           {
  82.                Button btn = new Button();
  83.                btn.Parent = this;
  84.                btn.Text   = astrButton[i];
  85.                btn.Location = new Point(100 + 72 * (i > 3 ? 1 : 0), 
  86.                                         8 + (i % 4) * 24);
  87.                btn.Size     = new Size(64, 14);
  88.      
  89.                if (i == 0)    // Reset button
  90.                {
  91.                     AcceptButton = btn;
  92.                }
  93.                if (i == 1)    // Invert button
  94.                {
  95.                     btnInvert = btn;
  96.                }
  97.                if (i < 7)     // All buttons except Cancel
  98.                {
  99.                     btn.Click += aeh[i];
  100.                     btn.DialogResult = DialogResult.OK;
  101.                }
  102.                else           // Cancel button
  103.                {
  104.                     btn.DialogResult = DialogResult.Cancel;
  105.                     CancelButton = btn;
  106.                }
  107.           }
  108.           ClientSize = new Size(240, 106);
  109.  
  110.           AutoScaleBaseSize = new Size(4, 8);
  111.      }
  112.      public Matrix Matrix 
  113.      {
  114.           get 
  115.           { 
  116.                return matrix; 
  117.           } 
  118.           set 
  119.           { 
  120.                matrix = value;
  121.                btnInvert.Enabled = matrix.IsInvertible;
  122.           } 
  123.      }
  124.      void ButtonResetOnClick(object obj, EventArgs ea)
  125.      {
  126.           matrix.Reset();
  127.      }
  128.      void ButtonInvertOnClick(object obj, EventArgs ea)
  129.      {
  130.           matrix.Invert();
  131.      }
  132.      void ButtonTranslateOnClick(object obj, EventArgs ea)
  133.      {
  134.           matrix.Translate((float) updown[0].Value, 
  135.                            (float) updown[1].Value,
  136.                radio[0].Checked ? MatrixOrder.Prepend : MatrixOrder.Append);
  137.      }
  138.      void ButtonScaleOnClick(object obj, EventArgs ea)
  139.      {
  140.           matrix.Scale((float) updown[0].Value, 
  141.                        (float) updown[1].Value,
  142.                radio[0].Checked ? MatrixOrder.Prepend : MatrixOrder.Append);
  143.      }
  144.      void ButtonRotateOnClick(object obj, EventArgs ea)
  145.      {
  146.           matrix.Rotate((float) updown[2].Value,
  147.                radio[0].Checked ? MatrixOrder.Prepend : MatrixOrder.Append);
  148.      }
  149.      void ButtonRotateAtOnClick(object obj, EventArgs ea)
  150.      {
  151.           matrix.RotateAt((float) updown[2].Value, 
  152.                           new PointF((float) updown[0].Value, 
  153.                                      (float) updown[1].Value),
  154.                radio[0].Checked ? MatrixOrder.Prepend : MatrixOrder.Append);
  155.      }
  156.      void ButtonShearOnClick(object obj, EventArgs ea)
  157.      {
  158.           matrix.Shear((float) updown[0].Value, 
  159.                        (float) updown[1].Value,
  160.                radio[0].Checked ? MatrixOrder.Prepend : MatrixOrder.Append);
  161.      }
  162. }